using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using FredCK.FCKeditorV2;
using CMS.BIZ;

namespace CMS.Web.UI.UserControls
{
    public class ContentEditor : UserControl
    {

        private BizContent content = null;
        private int? contentID = null;

        protected DropDownList ddlCulture;
        protected FCKeditor wysiwygEditor;
        protected LinkButton lbCreateContent;

        protected Button bValidate;

        public BizContent Content
        {
            get { return content; }
            set { content = value; }
        }
        public int? ContentID
        {
            get { return contentID; }
            set { contentID = value; }
        }

        public ContentEditor()
        {
            base.Init += new EventHandler(ContentEditor_Init);
            base.Load += new EventHandler(ContentEditor_Load);
        }

        protected void ContentEditor_Init(object sender, EventArgs e)
        {
            bValidate.Click += new EventHandler(Validate_Click);
            lbCreateContent.Click += new EventHandler(CreateContent_Click);
            ddlCulture.SelectedIndexChanged += new EventHandler(Culture_SelectedIndexChanged);
        }
        protected void CreateContent_Click(object sender, EventArgs e)
        {
            if (content == null)
            {
                content = new BizContent();
                if (contentID != null) content.ContentID = (int)contentID;
                else content.ContentID = Page.BizContentProvider.GetMaxID() + 1;
                content.CultureCode = ddlCulture.SelectedValue;
                contentID = content.ContentID;
                Page.BizContentProvider.Create(content);
            }
        }
        protected void ContentEditor_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetCultures();
                ddlCulture.SelectedValue = Page.CultureCode;
            }
            ReadContent();
            if (!IsPostBack)
            {
                SetFieldsValue();
            }
        }
        protected void Culture_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetFieldsValue();
        }
        protected void Validate_Click(object sender, EventArgs e)
        {
            if (content != null)
            {
                content.ContentText = wysiwygEditor.Value;
                Page.BizContentProvider.Update(content);
            }
        }
        private void SetCultures()
        {
            ddlCulture.DataTextField = "CultureName";
            ddlCulture.DataValueField = "CultureCode";
            ddlCulture.DataSource = Page.BizCultureProvider.ReadAll();
            ddlCulture.DataBind();
        }
        private void ReadContent()
        {
            if (content != null) return;
            string strContentID = Request.QueryString["ContentID"];
            if (contentID == null && !string.IsNullOrEmpty(strContentID)) 
            {
                contentID = int.Parse(strContentID);
            }
            if (contentID != null)
            {
                int id = (int)contentID;
                content = Page.BizContentProvider.Find(id, ddlCulture.SelectedValue);
            }
        }
        private void SetFieldsValue()
        {
            if (content != null)
            {
                wysiwygEditor.Value = content.ContentText;
                wysiwygEditor.Visible = true;
                bValidate.Visible = true;
                lbCreateContent.Visible = false;
            }
            else
            {
                wysiwygEditor.Visible = false;
                bValidate.Visible = false;
                lbCreateContent.Visible = true;
            }
        }
    }
}
